﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace gTray
{
    public partial class frmNotify : Form
    {
        private const int WM_PRINT = 0x0317;

        public event EventHandler NotificationFinished;
        private Timer _Timer;

        private Feed _Feed;

        private int _Index;

        public frmNotify(Feed feed)
        {
            InitializeComponent();
            this.DoubleBuffered = true;

            this.BackColor = Color.White;

            this.Location = DetermineStartLocation();

            this.TopMost = true;

            _Timer = new Timer();
            _Timer.Interval = (int)TimeSpan.FromSeconds(SettingService.Instance.NotificationIntervalSeconds).TotalMilliseconds;
            _Timer.Tick += new EventHandler(_Timer_Tick);

            this.MouseEnter += new EventHandler(frmNotify_MouseEnter);
            this.MouseLeave += new EventHandler(frmNotify_MouseLeave);

            foreach (Control cont in this.Controls)
            {
                cont.MouseEnter += new EventHandler(frmNotify_MouseEnter);
                cont.MouseLeave += new EventHandler(frmNotify_MouseLeave);
            }

            _Feed = feed;

            UpdateLabels();
            ToggleButtons();
        }

        private void UpdateLabels()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("You have ");
            builder.Append(_Feed.MaxUnreadCount);
            
            if (_Feed.NewEntries.Count == 1)
                builder.Append(" New Unread Email!");
            else
                builder.Append(" New Unread Emails!");

            lblLinkUnread.Text = builder.ToString();

            lblLinkEmail.Text = _Feed.NewEntries[_Index].Title;

            builder = new StringBuilder();

            builder.Append("From: ");
            builder.Append(_Feed.NewEntries[_Index].AuthorName);
            builder.AppendLine();

            builder.Append(_Feed.NewEntries[_Index].Summary);

            lblAuthors.Text = builder.ToString();
        }

        void frmNotify_MouseLeave(object sender, EventArgs e)
        {
            _Timer.Start();
        }

        void frmNotify_MouseEnter(object sender, EventArgs e)
        {
            _Timer.Stop();
        }

        private Point DetermineStartLocation()
        {
            Utilities.TaskbarLocation taskLocal = Utilities.FindTaskbarLocation();
            Point pnt = new Point();

            if (taskLocal == Utilities.TaskbarLocation.Bottom)
            {
                pnt.X = SystemInformation.WorkingArea.Width - this.Width;
                pnt.Y = SystemInformation.WorkingArea.Height - this.Height;

            }

            return pnt;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Utilities.AnimateWindow(this.Handle, 500, 
                Utilities.AnimateWindowFlags.AW_SLIDE | 
                Utilities.AnimateWindowFlags.AW_VER_NEGATIVE);
            _Timer.Start();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (e.Cancel == false)
            {
                Utilities.AnimateWindow(this.Handle, 500,
                    Utilities.AnimateWindowFlags.AW_SLIDE |
                    Utilities.AnimateWindowFlags.AW_VER_POSITIVE |
                    Utilities.AnimateWindowFlags.AW_HIDE);
            }
        }

        void _Timer_Tick(object sender, EventArgs e)
        {
            this.Close();
            _Timer.Stop();

            if (NotificationFinished != null)
                NotificationFinished(this, EventArgs.Empty);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics,
                new Rectangle(0, 0, this.Width -1, this.Height -1),
                Color.Red, 
                ButtonBorderStyle.Outset);

            base.OnPaint(e);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                // Turn on WS_EX_TOOLWINDOW style bit
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x80;
                return cp;
            }
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_PRINT)
            {
                if (!this.Visible)
                {
                    this.Visible = true;
                }
            }

            base.WndProc(ref m);
        }

        private void lblLinkUnread_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(_Feed.Link.AbsoluteUri);
        }

        private void lblLinkEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(_Feed.NewEntries[_Index].Link.AbsoluteUri);
        }

        #region Button Management
        private void btnBack_Click(object sender, EventArgs e)
        {
            if(_Index > 0)
                _Index--;

            ToggleButtons();

            UpdateLabels();
        }

        private void btnFoward_Click(object sender, EventArgs e)
        {
            if (_Index < _Feed.NewEntries.Count)
                _Index++;

            ToggleButtons();

            UpdateLabels();
        }

        private void ToggleButtons()
        {
            btnBack.Enabled = true;
            btnFoward.Enabled = true;

            if (_Index == 0)
                btnBack.Enabled = false;
            if (_Index == _Feed.NewEntries.Count - 1)
                btnFoward.Enabled = false;
        }
        #endregion
    }
}
